package com.vedana.bankmobile.shared.protobuf.serializers;

import java.util.ArrayList;
import java.util.List;

import com.vedana.bankmobile.shared.Account;
import com.vedana.bankmobile.shared.protobuf.BankProto.AccountMsg;

/**
 * Serialize Account domain objects to protobuf messages
 * 
 * @author Didier MOISE
 *
 */
public class AccountSerializer {
	/**
	 * Read Account POJO from protobuf message
	 * @param msg Protobuf message
	 * @return Account POJO
	 */
	public static Account readFromMsg(AccountMsg msg) {
		Account account = new Account();

		account.setId(msg.getId());
		account.setCustomerId(msg.getCustId());
		account.setLabel(msg.getLabel());
		account.setBalance(msg.getBalance());

		return account;
	}

	/**
	 * Write Account POJO to protobuf message
	 * @param account Account POJO
	 * @return Protobuf message
	 */
	public static AccountMsg writeToMsg(Account account) {
		AccountMsg msg = AccountMsg.newBuilder()
				.setId(account.getId())
				.setCustId(account.getCustomerId())
				.setLabel(account.getLabel())
				.setBalance(account.getBalance())
				.build();
		return msg;
	}

	/**
	 * Read List of account POJO from protobuf message
	 * @param msg Protobuf message
	 * @return Account POJO list
	 */
	public static List<Account> readListFromMsg(List<AccountMsg> msg) {
		List<Account> accounts = new ArrayList<Account>();
		for (AccountMsg m : msg) {
			accounts.add(readFromMsg(m));
		}
		return accounts;
	}

	/**
	 * Write Account POJO list to protobuf message
	 * @param accounts Account POJO list
	 * @return Protobuf message
	 */
	public static List<AccountMsg> writeListToMsg(List<Account> accounts) {
		List<AccountMsg> msg = new ArrayList<AccountMsg>();
		for (Account account : accounts) {
			msg.add(writeToMsg(account));
		}
		return msg;
	}
}
